'use strict'

require('log-timestamp');
const express = require('express');
const router = express.Router();

const speedtest = require('../models/speedtest');
const ap = speedtest.ap;
const ap_list = speedtest.ap_list;
const testResult = speedtest.testResult;

const index = require('../index');
const config = require('../config');
const model = require('../models/models');

const _myData = {
  ap_list: [
    { site: 'espl', location: 'lab', ssid: 'Ectivise_5G', password: '00001111', runtime: 0, desc: 'Test' }
  ],
  testResult: [{ ping: '46.995 ms', download: '53.90 Mbit/s', upload: '3.43 Mbit/s', ssid: 'Ectivise_5G', timestmp: 1587513600 }]
}

/**
 * The most important params are password
 */
router.get('/get-ssid-list', (req, res) => {
  let pwd = req.body.secret;
  if (pwd != config.secret)
    return res.json({
      errorCode: 0,
      message: 'adminsecret wrong!'
    });
  //authentication with secrete
  //get data from the mongodb
  let retdata = _myData;
  res.send({
    errorCode: 0,
    data: retdata
  })
})

/**
 * The most important params are password
 */
router.get('/api/get-ssid-info', (req, res) => {
  //authentication with secrete
  //get data from the mongodb
  let retdata = _myData;
  res.send({
    errorCode: 0,
    data: retdata
  })
})


/**
 * the rpi can scan all the access point and upload but many of them are not valid
 * so there'd be a question, setup and update the ap info
 *
 * The password must be setup at backend.
 *
 * How about others?
 *
 * mac should be the param uploaded by 'me'
 * the ap info should be setup at backend, the scanned result only include
 */
router.post('/update-ap', function (req, res) {
  //authentication with secrete
  if (req.body.ssid && req.body.timestmp) {
    _myData.testResult.push({ ping: req.body.ping, download: req.body.download, upload: req.body.upload, ssid: req.body.ssid, timestmp: req.body.timestmp })
    res.send({
      errorCode: 0,
      data: _myData
    })
  } else {
    res.send({
      errorCode: 1,
      message: 'wrong input params'
    })
  }
})

/**
 * the test result may be fail or the test data
 * if fail, just count the failure count
 */
router.post('/upload-testresult', function(req, res) {
  //authentication with secrete
  if (req.body.ssid && req.body.timestmp) {
    _myData.testResult.push({ ping: req.body.ping, download: req.body.download, upload: req.body.upload, ssid: req.body.ssid, timestmp: req.body.timestmp })
    res.send({
      errorCode: 0,
      data: _myData
    })
  } else {
    res.send({
      errorCode: 1,
      message: 'wrong input params'
    })
  }
})


router.post('/upload-testresult1', function(req, res) {
  //authentication with secrete
  if (req.body.ssid && req.body.timestmp) {
    _myData.testResult.push({ ping: req.body.ping, download: req.body.download, upload: req.body.upload, ssid: req.body.ssid, timestmp: req.body.timestmp })
    res.send({
      errorCode: 0,
      data: _myData
    })
  } else {
    res.send({
      errorCode: 1,
      message: 'wrong input params'
    })
  }
})

router.post('/getGifts', (req, res) => {
    Gift.find({}, (err, list) => {
        if (err) {
            return res.json({
                errorCode: 0,
                message: 'get gifts failure:' + err.message
            });
        }
        if(list&&list.length>0){
            res.json({
                'errorCode': 1,
                message: 'success',
                data: list
            });
        }else{
            res.json({
                errorCode: 0,
                message: 'no data'
            });
        }
    })
});


module.exports = router;
